package com.senyint.hip.fhir.web;

import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.impl.GenericClient;
import com.google.gson.Gson;
import com.senyint.hip.fhir.interceptor.CaptureInterceptor;
import com.senyint.hip.fhir.param.PatientVO;
import com.senyint.hip.fhir.util.DateUtils;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.exceptions.FHIRException;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author ChenPan
 * @date 2018/10/30 11:04
 */
@RestController
@RequestMapping("/patient")
public class PatientController extends FhirBaseController {

    @PostMapping()
    public String createPatient(@RequestBody PatientVO param) throws FHIRException {

        Patient p = convertPatient(param);

        CaptureInterceptor interceptor = new CaptureInterceptor();
        GenericClient client = newClient(tempData.getBaseServer(), interceptor);

        MethodOutcome out = client.create().resource(p).execute();

        return new Gson().toJson(out);
    }

    @PutMapping()
    public String updatePatient(@RequestBody PatientVO param) throws FHIRException {

        Patient p = convertPatient(param);

        CaptureInterceptor interceptor = new CaptureInterceptor();
        GenericClient client = newClient(tempData.getBaseServer(), interceptor);

        MethodOutcome out = client.update(p.getId(), p);

        return new Gson().toJson(out);
    }


    protected Patient convertPatient(PatientVO param) throws FHIRException {
        Patient p = new Patient();
        p.setId(param.getId());
        System.out.println(param.getBirthTime());
        p.addName(new HumanName().setText(param.getPatientName()).setFamily(param.getPatientName()).addGiven(param.getPatientName()));
        p.setGender(Enumerations.AdministrativeGender.fromCode(param.getSex()));

        p.setBirthDate(DateUtils.date(param.getBirthDate())).getExtension()
                .add(new Extension()
                        .setUrl("http://hl7.org/fhir/StructureDefinition/patient-birthTime")
                        .setValue(new DateTimeType( DateUtils.string2Date(param.getBirthTime()))));
       

        ContactPoint ptel = new ContactPoint();
        ptel.setValue(param.getTelphone());
        p.addTelecom(ptel);

        //身份证
        p.addIdentifier().setSystem("2.16.840.1.113883.2.23.1.9.1").setValue(param.getIdentityNo());
        //社保卡
        p.addIdentifier().setSystem("2.16.840.1.113883.2.23.1.9.2").setValue(param.getInsuranceNo());
        //地址
        p.addAddress().setUse(Address.AddressUse.HOME).setText(param.getHomeAddr()).setPostalCode(param.getZipCode());

        //联系人
        CodeableConcept relationCode = new CodeableConcept();
        List<CodeableConcept> list = new ArrayList<>();
        relationCode.setText(param.getRelationShip());
        list.add(relationCode);

        //联系人电话
        ContactPoint tel = new ContactPoint();
        tel.setValue(param.getContactPhone());
        List<ContactPoint> ls = new ArrayList<>();
        ls.add(tel);

        p.addContact().setName(new HumanName()
                .setText(param.getContact()))
                .setRelationship(list)
                .setTelecom(ls);

        return p;
    }
}
